﻿using System;
using System.Data.SqlClient;
using sdamarketing.App_Code;

namespace sdamarketing
{
    public partial class posts : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) { pnlVideoOpis.Visible = false; pnlVideoURL.Visible = false; }
            if (IsPostBack)
            {
                if (chkBxYoutube.Checked == true)
                {
                    pnlVideoOpis.Visible = true; pnlVideoURL.Visible = true;
                }
                if (chkBxYoutube.Checked == false)
                {
                    pnlVideoOpis.Visible = false; pnlVideoURL.Visible = false;
                }
                
            }
        }

        protected void btnPost_Click(object sender, EventArgs e)
        {
            if (txtbVideo.Text!=null & txtbVideo.Text.Length>31)
            {
                string YoutubeURL = txtbVideo.Text;
                string YoutubeID = YoutubeURL.Remove(0,31);
                string YoutubeZaDisplay = "<object width='425' height='355'><param name='movie' value='http://www.youtube.com/v/" + YoutubeID + "'></param><param name='wmode' value='transparent'></param><embed src='http://www.youtube.com/v/" + YoutubeID + "' type='application/x-shockwave-flash' wmode='transparent' width='425' height='355'></embed></object>";

                var postce = new Post();
                postce.addStatus(FreeTextBox2.Text, Int32.Parse(Session["iduser"].ToString()), YoutubeZaDisplay, txtVideoOpis.Text);
                
                //Snimanje na TEKSTOT od Postot vo PodatociZaAnaliza
                SqlConnection connection = ConnectionMenager.GetConnection();
                String sql = "INSERT INTO PodatociZaAnaliza (idUser,textZaAnaliza) VALUES ('" + Session["idUser"].ToString() + "' , '" + txtVideoOpis.Text + "')";
                SqlCommand command = new SqlCommand(sql, connection);
                command.ExecuteNonQuery();
                connection.Close();
                if (Session["MyCulture"].ToString() == "en-US")
                {
                    lblMessage.Text = "Post succesfull.";
                }
                else
                {
                    lblMessage.Text = "Постот е внесен.";
                }
                

                FreeTextBox2.Text = "";
                chkBxYoutube.Checked = false;
                txtbVideo.Text = "";
                txtVideoOpis.Text = "";
                pnlVideoOpis.Visible = false;
                pnlVideoURL.Visible = false;
            }
            else
            {
                var postce = new Post();
                postce.addStatus(FreeTextBox2.Text, Int32.Parse(Session["iduser"].ToString()));
                //Snimanje na TEKSTOT od Postot vo PodatociZaAnaliza
                SqlConnection connection = ConnectionMenager.GetConnection();
                String sql = "INSERT INTO PodatociZaAnaliza (idUser,textZaAnaliza) VALUES ('" + Session["idUser"].ToString() + "' , '" + FreeTextBox2.Text + "')";
                SqlCommand command = new SqlCommand(sql, connection);
                command.ExecuteNonQuery();
                connection.Close();
                lblMessage.Text = "Постот е внесен.";

                FreeTextBox2.Text = "";
                chkBxYoutube.Checked = false;
                txtbVideo.Text = "";
                txtVideoOpis.Text = "";
                pnlVideoOpis.Visible = false;
                pnlVideoURL.Visible = false;
            }
        }
    }
}